//
//  XTXgexingqianmingViewController.m
//  XZ_WeChat
//
//  Created by 余清巍 on 2017/9/7.
//  Copyright © 2017年 gxz. All rights reserved.
//

#import "XTXgexingqianmingViewController.h"
#import "SDAutoLayout.h"

#import "XTXTool.h"

@interface XTXgexingqianmingViewController ()<UITextViewDelegate>
@property (nonatomic,strong) UITextView *textView;//输入框
@property (nonatomic,strong) UILabel *zixianzhiLabel;//字限制
@end

@implementation XTXgexingqianmingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    self.view.backgroundColor= XZColor(240,240,244);
    self.title = @"编辑个性签名";
    //登陆按钮
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"后退按钮"] landscapeImagePhone:[UIImage imageNamed:@"后退按钮"] style:UIBarButtonItemStylePlain target:self action:@selector(fanhuishijian)];
    UIBarButtonItem *MoreItem= [[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(endEditEventClick)];
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIColor blackColor] forKey:NSForegroundColorAttributeName];
    [MoreItem setTitleTextAttributes:dic forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = MoreItem;
    backItem.tintColor = [UIColor blackColor];
    self.navigationItem.leftBarButtonItem = backItem;
    [self setUI];
    [self layout];
    [self jk_custom_RemoveString:@"" withType:@""];
}
-(NSString *)jk_custom_RemoveString:(NSString *)string withType:(NSString *)type{
    
    // 去掉首尾空格
    string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    if ([type isEqualToString:@"1"] || [type isEqualToString:@"2"]) {
        
        // 去掉所有空格和换行符
        string = [string stringByReplacingOccurrencesOfString:@"\r" withString:@""];
        string = [string stringByReplacingOccurrencesOfString:@"\n" withString:@""];
        
        if ([type isEqualToString:@"2"]){
            
            string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
        }
    }
    
    return string;
}
-(void)setUI{
    [self.view addSubview:self.textView];
    [self.view addSubview:self.zixianzhiLabel];
}
-(CGFloat)jk_widthTextContent:(NSString *)textContent withTextfont:(CGFloat)textfont withMaxSize:(CGSize)maxSize{
    
    return [self jk_sizeTextContent:textContent withSizeFont:textfont withMaxSize:maxSize].width;
}

-(void)layout{
    _textView.sd_layout.rightSpaceToView(self.view, 0).leftSpaceToView(self.view, 0).topSpaceToView(self.navigationController.navigationBar, 0).heightIs(150*App_Frame_Width/414);
    _zixianzhiLabel.sd_layout.rightSpaceToView(self.view, 10).topSpaceToView(_textView, 10*App_Frame_Width/414).heightIs(20*App_Frame_Width/414).widthIs(100*App_Frame_Width/414);
}
-(CGSize)jk_sizeTextContent:(NSString *)textContent withSizeFont:(CGFloat)textfont withMaxSize:(CGSize)maxSize{
    
    UIFont *font = [UIFont systemFontOfSize:textfont];
    NSDictionary *attrs = @{NSFontAttributeName:font};
    return [textContent boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading |NSLineBreakByCharWrapping attributes:attrs context:nil].size;
}
//返回
-(void)fanhuishijian{
    [self jk_sizeAttributedTextContentToFit:nil withMaxSize:CGSizeZero];
    [self.navigationController popViewControllerAnimated:YES];
}
-(CGSize)jk_sizeAttributedTextContentToFit:(NSMutableAttributedString *)attributedText withMaxSize:(CGSize)maxSize{
    
    UILabel *tempLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, maxSize.width, maxSize.height)];
    tempLabel.attributedText = attributedText;
    tempLabel.numberOfLines = 0;
    [tempLabel sizeToFit];
    return tempLabel.frame.size;
}
//完成
-(void)endEditEventClick{
    if (_textView.text.length>0) {
        NSDictionary *dic = @{@"info":_textView.text};
        [[NSNotificationCenter defaultCenter] postNotificationName:@"QM" object:nil userInfo:dic];
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        dispatch_async(dispatch_get_main_queue(), ^{
            [XTXTool showError:@"个性签名不能为空哦！"];
        });
        [self jk_changeCorlorWithColor:UIColor.clearColor TotalString:@"" SubStringArray:@[]];
    }
    
}
- (NSMutableAttributedString *)jk_changeCorlorWithColor:(UIColor *)color TotalString:(NSString *)totalStr SubStringArray:(NSArray *)subArray {
    
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:totalStr];
    for (NSString *rangeStr in subArray) {
        
        NSMutableArray *array = [self jk_getRangeWithTotalString:totalStr SubString:rangeStr];
        
        for (NSNumber *rangeNum in array) {
            
            NSRange range = [rangeNum rangeValue];
            [attributedStr addAttribute:NSForegroundColorAttributeName value:color range:range];
        }
        
    }
    
    return attributedStr;
}
//控制输入文字的长度和内容，可通调用以下代理方法实现
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    _zixianzhiLabel.text = [NSString stringWithFormat:@"%ld/40",range.location+1];
    if (range.location>=39)
    {
        //控制输入文本的长度
        return  NO;
    }else
    {
        return YES;
    }
}
- (NSMutableArray *)jk_getRangeWithTotalString:(NSString *)totalString SubString:(NSString *)subString {
    
    NSMutableArray *arrayRanges = [NSMutableArray array];
    
    if (subString == nil && [subString isEqualToString:@""]) {
        return nil;
    }
    
    NSRange rang = [totalString rangeOfString:subString];
    
    if (rang.location != NSNotFound && rang.length != 0) {
        
        [arrayRanges addObject:[NSNumber valueWithRange:rang]];
        
        NSRange   rang1 = {0,0};
        NSInteger location = 0;
        NSInteger   length = 0;
        
        for (int i = 0;; i++) {
            
            if (0 == i) {
                
                location = rang.location + rang.length;
                length = totalString.length - rang.location - rang.length;
                rang1 = NSMakeRange(location, length);
            } else {
                
                location = rang1.location + rang1.length;
                length = totalString.length - rang1.location - rang1.length;
                rang1 = NSMakeRange(location, length);
            }
            
            rang1 = [totalString rangeOfString:subString options:NSCaseInsensitiveSearch range:rang1];
            
            if (rang1.location == NSNotFound && rang1.length == 0) {
                
                break;
            } else {
                
                [arrayRanges addObject:[NSNumber valueWithRange:rang1]];
            }
        }
        return arrayRanges;
    }
    return nil;
}

-(UILabel *)zixianzhiLabel{
    if (!_zixianzhiLabel) {
        _zixianzhiLabel = [[UILabel alloc]init];
        _zixianzhiLabel.textColor = [UIColor blackColor];
        _zixianzhiLabel.text = [NSString stringWithFormat:@"%ld/40",_string.length];
        _zixianzhiLabel.textAlignment = NSTextAlignmentRight;
        _zixianzhiLabel.font = [UIFont systemFontOfSize:15];
    }
    return _zixianzhiLabel;
}
-(UITextView *)textView{
    if (!_textView) {
        self.automaticallyAdjustsScrollViewInsets=NO;
        _textView = [UITextView new];
        _textView.delegate = self;
        _textView.text = _string;
        //字体颜色
        _textView.textColor = [UIColor blackColor];
        _textView.backgroundColor = [UIColor whiteColor];
        //设置字体
        _textView.font = [UIFont systemFontOfSize:15];
        //设置是否可以滚动
    }
    return _textView;
}
@end
